#include <stdio.h>
#include <string.h>
#include "utils.h"
#include "sdf.h"

char HStr[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char Hex2Char(int i){
/*	
    if(i>=0 && i<= 9)
        return i + 0x30;
        //return i + '0'
    if(i>=10 && i<=15)
        return i + 0x37;
       // return  i + 'A' - 10;
*/
    return HStr[i];
}

int Bitstr2ByteArr(unsigned char * bs, unsigned char * ba){
    int blen=strlen(bs);
    // int mlen=(blen+7)/8+1('\0');
    int plen=(blen)%8;//非填充0的位数
    plen=(plen==0)?8:plen;
    char tmp=0;
    int i=0,j=0;
    while (i<blen)
    {
        while (i<blen&&plen)
        {
            tmp<<=1;
            tmp|=bs[i]-'0';
            i++;
            plen--;
        }
        plen=8;
        ba[j++]=tmp;
    }
    ba[j]='\0';
    return SDR_OK;
}

int ByteArr2Bitstr(unsigned char * ba, unsigned char * bs){
    int mlen=strlen(ba);
    // int blen=mlen*8+1('\0');    
    char tmp=0;
    for (int i=0;i<mlen;i++)
    {
        tmp=ba[i];
        for (int plen=8;plen!=0;plen--)
        {
            bs[i*8+plen-1]='0'+(tmp&1);
            tmp>>=1;
        }
    }
    ba[mlen*8]='\0';
    return SDR_OK;
}